﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Group16.Class_Library;
namespace Group16.Controller
{
    public partial class SearchInvoice : baseSearchForm
    {
        protected InvoiceManager Manager;
        public SearchInvoice()
        {
            InitializeComponent();
            SetCmbSource();
            SetGridViewSource();
        }

        private void SetCmbSource()
        {
            try
            {
                string comText = "Select InvoiceCode from Invoice";
                SqlDataReader read = (SqlDataReader)DataBaseInteraction.ExecuteCommand(comText, 2);
                while (read.Read())
                {
                    string s = read.GetString(0);
                    cmbKeyWord.AutoCompleteCustomSource.Add(s);
                }
            }
            catch (SqlException se)
            {
                MessageBox.Show("Problem in interaction with database" + se.Message);
            }
            finally
            {
                DataBaseInteraction.Connection.Close();
            }
        }
        protected void SetGridViewSource()
        {
            gridDetail.SuspendLayout();
            gridDetail.AutoGenerateColumns = false;
            gridDetail.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkCyan;

            DataGridViewColumn codeCol = new DataGridViewTextBoxColumn();
            codeCol.DataPropertyName = "InvoiceCode";
            codeCol.Name = "Invoice code";
            codeCol.ReadOnly = true;

            DataGridViewColumn totalCol = new DataGridViewTextBoxColumn();
            totalCol.DataPropertyName = "Total";
            totalCol.Name = "Total";
            totalCol.ReadOnly = true;

            DataGridViewColumn dateCol = new DataGridViewTextBoxColumn();
            dateCol.DataPropertyName = "DateMade";
            dateCol.Name = "Date made";
            dateCol.ReadOnly = true;

            gridDetail.Columns.AddRange(new DataGridViewColumn[] { codeCol, totalCol, dateCol });
            gridDetail.ResumeLayout();
        }
        protected override void Search()
        {
            string series = cmbKeyWord.Text;
            if (String.IsNullOrEmpty(series))
            {
                lbMessage.Text = "Please! Enter invoice's series needing searching!";
            }
            else
                lbMessage.Text = "";
            Manager = new InvoiceManager();
            Manager.SearchDevices(series);
            gridDetail.DataSource = Manager.Invoices;
        }

        protected override void CellMouseDown(DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right && e.RowIndex >=0 && e.ColumnIndex >=0)
            {
                ContextMenuStrip cxt = new ContextMenuStrip();

                ToolStripItem item1 = cxt.Items.Add("Devices");
                DataGridViewRow row    = gridDetail.Rows[e.RowIndex];

                DataGridViewCell cell = row.Cells["Invoice Code"];
                item1.Tag = cell.Value;
                item1.Click += new EventHandler(menuDevices_Click);

                ToolStripItem item2 = cxt.Items.Add("Customer");
                item2.Click += new EventHandler(menuCustomer_Click);
                item2.Tag = cell.Value;

                cxt.Show(MousePosition);
            }
        }
        private void menuDevices_Click(object sender, EventArgs e)
        {
            ToolStripItem item = sender as ToolStripItem;
            if (item != null)
            {
                string code = item.Tag as string;
                using (DeviceInvoice dv = new DeviceInvoice(code))
                {
                    dv.ShowDialog(); // Display a list devices in the invoice
                }
            }
        }
        private void menuCustomer_Click(object sender, EventArgs e)
        {
            ToolStripItem item = sender as ToolStripItem;
            if (item != null)
            {
                string ivcode = item.Tag as string;
                if (String.IsNullOrEmpty(ivcode)) return;
                using (CustomerInfoForm c = new CustomerInfoForm())
                {
                    Customer info = new Customer();
                    info.ReadDataBase(ivcode);
                    c.Customer = info;
                    c.SettingForm();
                    c.ShowDialog(); //Show the customer owning this invoice
                }
            }
        }
        
    }
}
